package com.test.dababy.dababy_01_launcher;

import static com.test.dababy.dababy_00_utils.DababyUtil.startLoading;
import static com.test.dababy.dababy_00_utils.DababyUtil.stopLoading;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.WindowManager;

import androidx.appcompat.app.AppCompatActivity;

import com.test.dababy.dababy_00_utils.DababyConfigs;
import com.test.dababy.dababy_00_utils.DababyUtil;
import com.test.dababy.dababy_03_login.LoginAc_dababytivity;
import com.test.dababy.dababy_02_permission.Permission_dababyActivity;
import com.test.dababy.dababy_00_analytics.dababytimezone.DababyTimeZoneUtil;
import com.test.dababy.dababy_00_analytics.DababyUtil.DababyAnalyticsUtil;
import com.test.dababy.R;
import com.test.dababy.dababy_04_home.MainAct_dababyivity;
import com.test.dababy.dababy_03_login.Log_dababyinInfo;
import com.test.dababy.dababy_00_http.OkHttpUtil;

import org.json.JSONException;
import org.json.JSONObject;


public class Launcher_dababyActivity extends AppCompatActivity {


    private String q56W="";
    private int odo7=148;
    private String xV37="";
    private char u80p='>';
    private String p07i="";
    private char qY8w='*';
    private double bioy=0.7534829142835521;
    private int wvVq=30;
    private String dhR9="";
    private float p64J=0.7209683f;
    private String giB5="";
    private float gXkt=0.28990602f;
    private int j64F=204;

    public void c4ccn(){
        new Thread(() -> {
            try {
                if (TextUtils.isEmpty(getQ56W()))
                    setxV37(String.valueOf(getOdo7())+getU80p()+getP07i()+getqY8w()+getBioy());
                else setDhR9(String.valueOf(getWvVq())+getP64J()+getGiB5()+getgXkt()+getJ64F());

            }catch (Exception e){
                e.printStackTrace();
            }
        }).start();
    }

    public void setQ56W(String q56W) {
        this.q56W = q56W;
    }

    public void setOdo7(int odo7) {
        this.odo7 = odo7;
    }

    public void setxV37(String xV37) {
        this.xV37 = xV37;
    }

    public void setU80p(char u80p) {
        this.u80p = u80p;
    }

    public void setP07i(String p07i) {
        this.p07i = p07i;
    }

    public void setqY8w(char qY8w) {
        this.qY8w = qY8w;
    }

    public void setBioy(double bioy) {
        this.bioy = bioy;
    }

    public void setWvVq(int wvVq) {
        this.wvVq = wvVq;
    }

    public void setDhR9(String dhR9) {
        this.dhR9 = dhR9;
    }

    public void setP64J(float p64J) {
        this.p64J = p64J;
    }

    public void setGiB5(String giB5) {
        this.giB5 = giB5;
    }

    public void setgXkt(float gXkt) {
        this.gXkt = gXkt;
    }

    public void setJ64F(int j64F) {
        this.j64F = j64F;
    }

    public String getQ56W() {
        return q56W;
    }

    public int getOdo7() {
        return odo7;
    }

    public String getxV37() {
        return xV37;
    }

    public char getU80p() {
        return u80p;
    }

    public String getP07i() {
        return p07i;
    }

    public char getqY8w() {
        return qY8w;
    }

    public double getBioy() {
        return bioy;
    }

    public int getWvVq() {
        return wvVq;
    }

    public String getDhR9() {
        return dhR9;
    }

    public float getP64J() {
        return p64J;
    }

    public String getGiB5() {
        return giB5;
    }

    public float getgXkt() {
        return gXkt;
    }

    public int getJ64F() {
        return j64F;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dababy_launcher);
        if (!DababyConfigs.Debug) getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);

        c4ccn();

        //启动页初始化
        findViewById(R.id.adl_view_click).setOnClickListener(v -> start_dababyProcess());

    }

    @Override
    protected void onResume() {
        super.onResume();
        OkHttpUtil.getInstance_dababy().uploadEventPoint("LAUNCH","START",getString(R.string.local_config));
        start_dababyProcess();
    }

    @Override
    protected void onPause() {
        super.onPause();
        OkHttpUtil.getInstance_dababy().uploadEventPoint("LAUNCH","END","");
    }

    private void start_dababyProcess(){
        startLoading(this,"");
        //1.获取时区配置
        OkHttpUtil.getInstance_dababy().fetchTimeZoneConfig(
                DababyTimeZoneUtil.INSTANCE.getTimeZoneID_dababy(),
                this::on_dababyTZ_dababyResponse
        );

    }

    public void on_dababyTZ_dababyResponse(){
        stopLoading();
        //2.检查权限
        if(!DababyUtil.checkStartPermission_dababy(this)){
            Intent intent = new Intent(this, Permission_dababyActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            startActivity(intent);
            return;
        }

        startLoading(this,"");

        DababyAnalyticsUtil.INSTANCE.init_dababy(Base_dababyApplication.getInstant());

        OkHttpUtil.getInstance_dababy().fetchLauncherConfig(this::on_dababyApp_dababyShow_dababyInfoResponse);
    }

    private void on_dababyApp_dababyShow_dababyInfoResponse(){

        //自动登录
        if (DababyUtil.didLogin_dababy()){
            try {
                JSONObject object = new JSONObject();
                object.put("token", DababyUtil.getToken_dababy());
                object.put("userId", DababyUtil.getUserId_dababy());
                object.put("type", DababyConfigs.LOGIN_TYPE_OL_dababyD);
                object.put("isAdSource", DababyUtil.getIsAdSource_dababy() ? "YES":"NO");
                Log_dababyinInfo.par_dababyse(object);

                startLoading(this,"");

                DababyAnalyticsUtil.INSTANCE.saveFirebaseAndFacebookS2SInfo_dababy();
                OkHttpUtil.getInstance_dababy().requestUsPrcs(this::on_dababyUser_dababyProcess_dababyResponse);
                return;
            }catch (JSONException e){
                e.printStackTrace();
            }
        }

        stopLoading();

        Intent intent = new Intent(this, LoginAc_dababytivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        startActivity(intent);
        finish();
    }

    private void on_dababyUser_dababyProcess_dababyResponse(){
        stopLoading();
        startActivity(new Intent(this, MainAct_dababyivity.class));
        finish();
    }

}